#include "dialog.h"

Dialog::Dialog(QWidget *parent)
{
    //文件对话框相关控件
    fileBtn = new QPushButton;
    fileBtn->setText(tr("File Dialog Demo"));
    fileLineEdit = new QLineEdit;

    //颜色对话框相关控件
    colorBtn = new QPushButton;
    colorBtn->setText(tr("Color Dialog Demo"));
    colorFrame = new QFrame;
    colorFrame->setFrameShape(QFrame::Box);
    colorFrame->setAutoFillBackground(true);

    //字体对话框相关控件
    fontBtn = new QPushButton;
    fontBtn->setText(tr("Font Dialog Demo"));
    fontLineEdit = new QLineEdit;
    fontLineEdit->setText(tr("Hello World!"));

    mainLayout = new QGridLayout(this);
    //文件相关布局
    mainLayout->addWidget(fileBtn, 0, 0);
    mainLayout->addWidget(fileLineEdit, 0, 1);
    mainLayout->addWidget(colorBtn, 1, 0);
    mainLayout->addWidget(colorFrame, 1, 1);
    //颜色相关布局
    mainLayout->addWidget(colorBtn, 1, 0);
    mainLayout->addWidget(colorFrame, 1, 1);
    //字体相关布局
    mainLayout->addWidget(fontBtn, 2, 0);
    mainLayout->addWidget(fontLineEdit, 2, 1);

    connect(fileBtn, SIGNAL(clicked()), this, SLOT(showFile()));
    connect(colorBtn, SIGNAL(clicked()), this, SLOT(showColor()));
    connect(fontBtn, SIGNAL(clicked()), this, SLOT(showFont()));
}

void Dialog::showFile()
{
    //显示标准文件对话框
    QString fileName = QFileDialog::getOpenFileName(this, "Open File Dialog", "/", "C++ files(*.cpp);;C files(*.c);;Head files(*.h)");
    fileLineEdit->setText(fileName);
}

void Dialog::showColor()
{
    //显示颜色选择对话框
    QColor color = QColorDialog::getColor(Qt::blue);
    if(color.isValid())
    {
        colorFrame->setPalette(QPalette(color));
    }
}

void Dialog::showFont()
{
    //显示字体选择对话框
    bool ok;
    QFont font = QFontDialog::getFont(&ok);
    if(ok)
    {
        fontLineEdit->setFont(font);
    }
}

Dialog::~Dialog()
{}
